package com.lzw.d25bingso;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lzw.d25bingso.adapter.IndustryAdapter;
import com.lzw.d25bingso.bean.Industry;
import com.lzw.d25bingso.bean.Viewpagers;
import com.lzw.d25bingso.callback.DataCallBack;
import com.lzw.d25bingso.task.DataLoadTask;
import com.lzw.d25bingso.task.ImageDownLoad;
import com.lzw.d25bingso.task.IndustryDataLoadTask;
import com.lzw.d25bingso.tools.Config;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzw on 2017/5/14.
 */

 public class MyFragment extends Fragment{

    private ViewPager viewPager;
    private LinearLayout layout; //存放指示器的容器13
    private List<ImageView> imageData;//ViewPager 的数据源
    private ImageView [] icons;//指示器数据源
    private ImageAdapter adapter;

    private GridView gv;
    private List<Industry> gvData;
    private IndustryAdapter industryAdapter;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
                handler.sendEmptyMessageDelayed(1,2000);
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_home,container,false);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager_id3);
        layout = (LinearLayout) view.findViewById(R.id.image_layout_id);

        gv = (GridView) view.findViewById(R.id.gv);
        gvData = new ArrayList<>();
        industryAdapter = new IndustryAdapter(getContext(),gvData);
        gv.setAdapter(industryAdapter);
        new IndustryDataLoadTask(new DataCallBack() {
            @Override
            public void sendData(List<Viewpagers> data) {

            }

            @Override
            public void sendIndusData(List<Industry> data) {
                gvData.addAll(data);
                industryAdapter.notifyDataSetChanged();
            }
        }).execute(Config.INDUSTRY_PATH);

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String id1 = gvData.get(position).getId();
                String name = gvData.get(position).getName();
                Intent intent = new Intent(getContext(),CompanyActiity.class);
                intent.putExtra("id",id1);
                intent.putExtra("name",name);
                startActivity(intent);
            }
        });
        imageData = new ArrayList<>();
        new DataLoadTask(new DataCallBack() {
            @Override
            public void sendData(List<Viewpagers> data) {

                icons = new ImageView[data.size()];
                for (int i = 0;i<data.size();i++){
                    ImageView imageView = new ImageView(getContext());
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    String banner = data.get(i).getBanner();
                    new ImageDownLoad(imageView).execute(banner);
                    imageData.add(imageView);

                    icons[i] = new ImageView(getContext());
                    icons[i].setImageResource(R.mipmap.white);
                    icons[i].setPadding(5,0,5,0);
                    layout.addView(icons[i]);
                }
                icons[0].setImageResource(R.mipmap.blue);

                viewPager.setAdapter(new ImageAdapter());
                viewPager.setCurrentItem(imageData.size()*300 / 2 - imageData.size()*300 / 2 % imageData.size());//TODO
                handler.sendEmptyMessageDelayed(1, 2000);//TODO

                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        int dex = position%imageData.size();//TODO
                        for (int i = 0;i<icons.length;i++){
                            icons[i].setImageResource(R.mipmap.white);
                        }
                        icons[dex].setImageResource(R.mipmap.blue);//TODO
//                        icons[position].setImageResource(R.mipmap.blue);
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });

            }

            @Override
            public void sendIndusData(List<Industry> data) { }



        }).execute(Config.VIEW_PAGER_PATH);

    viewPager.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int dex = viewPager.getCurrentItem()%imageData.size();
            Toast.makeText(getContext(),"---"+dex,Toast.LENGTH_LONG).show();
            Intent intent = new Intent();
            switch (event.getAction()){
                case MotionEvent.ACTION_UP :
                    if (dex ==0){
                        intent.setClass(getContext(),CeShiActivity.class);
                        startActivity(intent);
                    }else if (dex==1){
                        Toast.makeText(getContext(),"---"+1,Toast.LENGTH_LONG).show();
                    }else if (dex==2){
                        Toast.makeText(getContext(),"---"+2,Toast.LENGTH_LONG).show();
                    }else if (dex==3){
                        Toast.makeText(getContext(),"---"+3,Toast.LENGTH_LONG).show();
                    }

            }

            return false;
        }
    });


        return view;
    }
    public class ImageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
//            return Integer.MAX_VALUE;//TODO
            return  imageData.size()*300;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            int dex= position%imageData.size();//TODO
            container.addView(imageData.get(dex));//TODO
            return imageData.get(dex);//TODO

//            container.addView(imageData.get(position));
//            return imageData.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            int dex= position%imageData.size();//TODO
            container.removeView(imageData.get(dex));//TODO
//            container.removeView(imageData.get(position));
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }




}
